#帖子私信页
from xml.etree.ElementTree import Element

from selenium.common import TimeoutException

from common.com import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


class ForumPM:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver

    # 帖子详情页私信(xiaohe1发送私信)
    def PostDetailPM(self):
        # 点击C++板块
        #self.driver.find_element( By.CSS_SELECTOR, "#topBoardList > li.nav-item.active > a" )
        # 点击第一个自动化测试帖子,进入帖子详情页

        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a").click()

        #self.driver.find_element( By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate" ).click()
        time.sleep( 1 )
        # 点击私信,用显式等待，确保元素完全加载且可交互，同时处理元素可见性（如滚动到视图）
        pm_button = WebDriverWait( self.driver, 10 ).until(EC.presence_of_element_located( (By.CSS_SELECTOR, "#btn_details_send_message") ))
        #pm_button = WebDriverWait( self.driver, 10 ).until(EC.presence_of_element_located( (By.XPATH, "/html/body/div[1]/header[1]/div/div/div[2]/div/a") ))
        # 滚动到元素（避免被遮挡）
        self.driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});",pm_button)
        # 再次等待可点击（滚动后可能需重新计算）
        WebDriverWait( self.driver, 5 ).until(EC.element_to_be_clickable( (By.CSS_SELECTOR,"#btn_details_send_message" ) )).click()
        # 输入私信内容
        self.driver.find_element( By.CSS_SELECTOR, "#index_message_receive_content" ).send_keys( "作者写的真好" )
        ForumDriver.getScreenShot()
        time.sleep( 1 )
        # 点击发送私信按钮
        self.driver.find_element( By.CSS_SELECTOR, "#btn_index_send_message" ).click()
        time.sleep( 1 )
        # 检查是否发送成功
        try:
            # 等待弹窗出现（设置超时时间，如3秒，与弹窗自动消失时间匹配或稍短）
            alert = WebDriverWait( self.driver, 3 ).until( EC.alert_is_present() )

            # 获取弹窗文本并打印
            alert_text = alert.text
            print( f"弹窗内容: {alert_text}" )
        except TimeoutException:
            print( "等待弹窗超时，未检测到弹窗" )
        ForumDriver.getScreenShot()

    # 回复私信(xiaohe接收私信)
    def ReplyPM(self):
        #论坛首页
        self.driver.find_element( By.CSS_SELECTOR, "#nav_board_index > a" ).click()
        # 点击私信按钮
        WebDriverWait( self.driver, 5 ).until( EC.element_to_be_clickable( (By.CSS_SELECTOR,
                                                                            "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a") ) )
        self.driver.find_element( By.CSS_SELECTOR,
                                  "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a" ).click()
        time.sleep( 1 )

        # 点击第一个未读私信
        self.driver.find_element( By.CSS_SELECTOR,"#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a" ).click()
        time.sleep( 1 )
        # 点击回复按钮
        self.driver.find_element( By.CSS_SELECTOR, "#btn_index_message_reply" ).click()
        time.sleep( 1 )
        # 编辑回复内容
        self.driver.find_element( By.CSS_SELECTOR, "#index_message_reply_receive_content" ).send_keys( "谢谢宝宝喜欢o(*￣▽￣*)ブ" )
        time.sleep( 1 )
        # 点击发送按钮
        self.driver.find_element( By.CSS_SELECTOR, "#btn_index_send_message_reply" ).click()
        time.sleep( 1 )
        # 提示发送成功
        try:
            # 等待弹窗出现（设置超时时间，如3秒，与弹窗自动消失时间匹配或稍短）
            # alert = WebDriverWait( self.driver, 3 ).until( EC.alert_is_present() )
            alert = WebDriverWait(self.driver,3).until(EC.visibility_of_element_located
                                                       (By.CSS_SELECTOR,'body > div.jq-toast-wrap.bottom-right > div > h2'))
            # 获取弹窗文本并打印
            alert_text = alert.text
            print( f"弹窗内容: {alert_text}" )
        except TimeoutException:
            print( "等待弹窗超时，未检测到弹窗" )
        ForumDriver.getScreenShot()
        #关闭私信界面
        self.driver.find_element( By.CSS_SELECTOR,"#index_message_offcanvasEnd > div.offcanvas-header > button" ).click()


